<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/9
 * Time: 14:57
 */

namespace Admin\Ims\Controllers;
use \Admin\Frontend\Controllers\ControllerBase;
use Admin\Models\ims\XHImsProduce;
use Helpers\Assets;
use Helpers\Tool;
use Helpers\XhException;
use Modules\Customer;
use Modules\Ims;

class StatsController extends ControllerBase
{

    public static $enum = [
        'iInType'  => [1 => '采购入库', 2 => '退还入库', 3 => '维修返还'],
        'iOutType' => [1 => '登记出库', 2 => '申领出库', 3 => '流程计划出库', 4 => '维修出库'],
        'iDevType' => [
            2 => '学生机',
            3 => '教师机',
            4 => '备用机',
            5 => '公用机',
            6 => '自用机'
        ]
    ];

    /**
     * 库存统计
     *
     * @return \Phalcon\Mvc\View|string
     *
     * create by dale  20190110
     */
    public function indexAction()
    {
        try {
            $this->view->pick('stats/index');
            Assets::select2();
            Assets::importJs('js/stats/index.js');
            return $this->view->setVars([
                'province'  => Customer::getArea(0, Customer::TYPE_KEYVALUE),
                'startTime' => date('Y-m-d', strtotime('-1 year')),
                'endTime' => date('Y-m-d')
            ]);
        } catch (\Exception $e) {
            return $this->sendViewError(
                $e->getMessage()
            );
        }
    }

    /**
     * 统计表格
     *
     * @return \Phalcon\Mvc\View
     *
     * create by dale  20190118
     */
    public function statsTabAction()
    {
        $this->view->pick('stats/statsTab');
        Assets::importJs('js/stats/statsTab.js');
        return $this->view->setVars([
            
        ]);
    }


    /**
     * 各校库存统计
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface|\Phalcon\Mvc\View
     *
     * create by dale  20190115
     */
    public function schoolImsAction()
    {
        try {
            $this->view->pick('stats/schoolIms');
            Assets::select2();
            Assets::importJs('js/stats/schoolIms.js');
            return $this->view->setVars([
                'province'  => Customer::getArea(0, Customer::TYPE_KEYVALUE),
            ]);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }
    }

    /**
     * 获取客户分页
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20190116
     */
    public function getSchoolPageAction()
    {
        try {
            $page = $this->request->getPost('page', 'int');
            $size = $this->request->getPost('size', 'int');
            $key  = $this->request->getPost('key', 'string');
            $province  = $this->request->getPost('province', 'string');
            $city  = $this->request->getPost('city', 'string');
            $county  = $this->request->getPost('county', 'string');
            $data = Customer::getCustomerList([
                'page' => $page,
                'size' => $size,
                'schoolIdNeeded'  => true,
                'key' => $key,
                'fields' => ['iSchoolId', 'sSchoolName', 'iCUId'],
                'province' => $province != '全部省' ? $province : '',
                'city' => $city != '全部市' ? $city : '',
                'county' => $county != '全部区' ? $county : '',
            ]);
            $totalPage = ceil($data['total'] / $size);
            $totalPage = $totalPage ?: 1;
            $data['totalPage']   = $totalPage;
            $data['currentPage'] = $page;

            return $this->sendJsonResponse($data);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }
    }


    /**
     * 折线图
     *
     * @return \Phalcon\Mvc\View|string
     *
     * create by dale  20190111
     */
    public function polyLineAction()
    {
        try {
//            $startTime = $this->request->getQuery(
//                'startTime',
//                'string',
//                date('Y-m-d', strtotime('-1 year'))
//            );
//            $endTime = $this->request->getQuery('endTime', 'string', date('Y-m-d'));
            $schoolId = $this->request->getQuery('schoolId', 'string');
            if ($schoolId) {
                $schoolName = Customer::getSchoolNameBySchoolId($schoolId);
                if ($schoolName) {
                    $this->view->setVar('schoolName', $schoolName);
                }
            }
            $this->view->pick('stats/polyLine');
            Assets::xhChart();
            Assets::select2();
            Assets::sweetAlert2();
            Assets::importJs('js/stats/polyLine.js');
            return $this->view->setVars([
                'startTime' => date('Y-m-d', strtotime('-1 year')),
                'endTime' => date('Y-m-d'),
                'prodType'  => Ims::getProdType(),

            ]);
        } catch (\Exception $e) {
            return $this->sendViewError(
                $e->getMessage()
            );
        }
    }

    /**
     * 根据商品类型获取商品
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20190114
     */
    public function getProdByTypeAction()
    {
        try {
            $typeId = $this->request->getPost('typeId', 'string');
            if (!$typeId) {
                return $this->sendErrorResponse(400, '缺少参数');
            }
            $prod = Ims::getProdByTypeId($typeId);
            return $this->sendJsonResponse($prod);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }
    }


    /**
     * 获取地区
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20190114
     */
    public function getAreaByCodeAction()
    {
        try {
            $code = $this->request->getPost('code', 'string', 0);
            if (!$code) {
                return $this->sendJsonResponse([]);
            }
            $area = Customer::getArea($code, Customer::TYPE_EASYUI);
            return $this->sendJsonResponse($area);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }
    }


    /**
     * 获取
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20190114
     */
    public function getSourceAction()
    {
        try {
            $startTime = $this->request->getPost('startTime', 'string');
            $endTime   = $this->request->getPost('endTime', 'string');
            $province  = $this->request->getPost('province', 'string');
            $city      = $this->request->getPost('city', 'string');
            $county    = $this->request->getPost('county', 'string');
            $prodType  = $this->request->getPost('prodType', 'string');
            $prod      = $this->request->getPost('prod', 'string');
            $statsType = $this->request->getPost('statsType', 'string');
            $type      = $this->request->getPost('type', 'string');
            $sortType  = $this->request->getPost('sortType', 'string');
            $schoolId  = $this->request->getPost('schoolId', 'string');
//            $province  =  $province != '全部省' ? $province : '';
//            $city      =  $city != '全部市' ? $city : '';
//            $county    =  $county != '全部区' ? $county : '';
            if ($type == 'in') {
                $source = $this->getInStats([
                    'startTime' => $startTime,
                    'endTime' => $endTime,
                    'province' => $province,
                    'city' => $city,
                    'county' => $county,
                    'prodType' => $prodType,
                    'prod' => $prod,
                    'statsType' => $statsType,
                    'yType' => $sortType,
                    'schoolId' => $schoolId
                ]);
            } elseif ($type == 'out') {
                $source = $this->getOutStats([
                    'startTime' => $startTime,
                    'endTime' => $endTime,
                    'province' => $province,
                    'city' => $city,
                    'county' => $county,
                    'prodType' => $prodType,
                    'prod' => $prod,
                    'statsType' => $statsType,
                    'yType' => $sortType,
                    'schoolId' => $schoolId
                ]);
            } elseif ($type == 'all') {
                $source = $this->getAllStatus([
                    'startTime' => $startTime,
                    'endTime' => $endTime,
                    'province' => $province,
                    'city' => $city,
                    'county' => $county,
                    'prodType' => $prodType,
                    'prod' => $prod,
                    'statsType' => $statsType,
                    'yType' => $sortType,
                    'schoolId' => $schoolId
                ]);
            } else {
                return $this->sendErrorResponse(500, '未知类型');
            }
            return $this->sendJsonResponse($source);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }



//        return $this->sendJsonResponse([
//            "title" =>  ['text' => "图表标题"],
////            "yTitle" => "y轴标题",
////            "xBaseCount" => 2,
//            'customerArr' => [5,6,7],
//            'followArr' => [4,6,8],
//            'series' => [
//                ['name' => '出库1','source' => 'customerArr'],
//                ['name' => '出库2','source' => 'followArr'],
//            ],
//            'xList' => [
//                '2018年第1个月',
//                '2018年第2个月',
//                '2018年第3个月'
//            ]
//        ]);
    }

    /**
     * 获取分类类型
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20190115
     */
    public function getSortTypeAction()
    {
        try {
            $type = $this->request->getPost('type', 'string');
            if (!$type) {
                return $this->sendErrorResponse('400', '缺少参数');
            }
            if ($type == 'in') {
                $arr = [
                    'iProdId'  => '商品类型',
                    'iInType'  => '入库类型',
                    'iDevType' => '设备类型',
                ];
            } elseif ($type == 'out') {
                $arr = [
                    'iProdId'  => '商品类型',
                    'iOutType'  => '出库类型',
                    'iDevType' => '设备类型',
                ];
            } elseif ($type == 'all') {
                $arr = [
                    'iProdId'  => '商品类型',
                    'iDevType' => '设备类型'

                ];
            } else {
                throw new \Exception('未知类型');
            }
            return $this->sendJsonResponse($arr);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }
    }

    /**
     * 获取总数据
     *
     * @param $params
     *
     * @return array
     * @throws \Exception
     *
     * create by dale  20190114
     */
    public function getAllStatus($params)
    {
        $startTime = Tool::getValue($params, 'startTime');
        $endTime   = Tool::getValue($params, 'endTime');
        $province  = Tool::getValue($params, 'province');
        $city      = Tool::getValue($params, 'city');
        $county    = Tool::getValue($params, 'county');
        $prodType  = Tool::getValue($params, 'prodType');
        $prod      = Tool::getValue($params, 'prod');
        $statsType = Tool::getValue($params, 'statsType');
        $yType     = Tool::getValue($params, 'yType');
        $schoolId  = Tool::getValue($params, 'schoolId');
        $xList = $this->getXList($statsType, $startTime, $endTime);
        if ($yType == 'iProdId') {
            $yList = Ims::getProdByTypeId($prodType);
        } else {
            $yList = @self::$enum[$yType];
        }

        if (!$yList) {
            throw new \Exception('未定义y轴分类');
        }
        $inSql = "select SUM(iInNum) as addNum,DATE_FORMAT(tCreateDate,'%Y-%m-%d') as tTime from XHIms_ProduceIn ";
        $outSql = "select SUM(iOutNum) as addNum,DATE_FORMAT(tCreateDate,'%Y-%m-%d') as tTime from XHIms_ProduceOut ";
        $conditionArr = [];
        $conditionArr[] = 'bDelete = 0 ';
        $prods = Ims::getProdByTypeId($prodType);
        if ($prod) {
            $conditionArr[] = " iProdId = {$prod} ";
        } elseif ($prodType) {
            $prodIds = array_keys($prods);
            if ($prodIds) {
                $conditionArr[] = " iProdId in (".implode(',', $prodIds).") ";
            }
        }
        if ($schoolId) {
            $conditionArr[] = "iSchoolId = '{$schoolId}' ";
        }
        $schoolIds = Customer::filterSchoolByArea([
            'province' => $province != '全部省' ? $province : '',
            'city' => $city != '全部市' ? $city : '',
            'county' => $county != '全部区' ? $county : '',
        ]);
        if ($schoolIds) {
            $conditionArr[] = " iSchoolId in (".implode(',', $schoolIds).") ";
        }
        if ($startTime) {
            $conditionArr[] = " DATE_FORMAT(tCreateDate,'%Y-%m-%d') >= '{$startTime}'  ";
        }
        if ($endTime) {
            $conditionArr[] = " DATE_FORMAT(tCreateDate,'%Y-%m-%d') <= '{$endTime}'  ";
        }

        $inSql .= 'where ' . implode(' and ', $conditionArr);
        $outSql .= 'where ' . implode(' and ', $conditionArr);
        $source = [
            'series' => [],
        ];
        $mixValue = 0;
        foreach ($yList as $index => $y) {
            $newSql = $inSql;
            $newSql.= " and {$yType} =  {$index} ";
            $newSql .= " GROUP BY DATE_FORMAT(tCreateDate,'%Y-%m-%d') ";
            $inData = $this->db->fetchAll($newSql);
            $newSql = $outSql;
            $newSql.= " and {$yType} =  {$index} ";
            $newSql .= " GROUP BY DATE_FORMAT(tCreateDate,'%Y-%m-%d') ";
            $outData = $this->db->fetchAll($newSql);
            $source['series'][] = [
                'name' => $y,
                'source' => 'dataSource' . $index,
            ];
            $inData = array_column($inData, 'addNum', 'tTime');
            $outData = array_column($outData, 'addNum', 'tTime');
            if ($outData) {
                foreach ($outData as $time => $num) { //合并数据
                    $dealNum = Tool::getValue($inData, $time, 0);
                    $inData[$time] = $dealNum - $num;
                }
            }
            $source['dataSource' . $index] = $this->getXData($xList, $inData, $statsType);
            foreach ($source['dataSource' . $index] as $val) {
                if ($val <= $mixValue) {
                    $mixValue = $val;
                }
            }
        }
        $source['xList'] = $statsType != 'day' ? array_keys($xList) : $xList;
        $source['yMin'] = $mixValue;
        return $source;
    }

    /**
     * 获取入库数据
     *
     * @param $params
     *
     * @return array
     * @throws \Exception
     *
     * create by dale  20190114
     */
    public function getInStats($params)
    {
        $startTime = Tool::getValue($params, 'startTime');
        $endTime   = Tool::getValue($params, 'endTime');
        $province  = Tool::getValue($params, 'province');
        $city      = Tool::getValue($params, 'city');
        $county    = Tool::getValue($params, 'county');
        $prodType  = Tool::getValue($params, 'prodType');
        $prod      = Tool::getValue($params, 'prod');
        $statsType = Tool::getValue($params, 'statsType');
        $yType     = Tool::getValue($params, 'yType');
        $xList     = $this->getXList($statsType, $startTime, $endTime);
        $schoolId  = Tool::getValue($params, 'schoolId');
        if ($yType == 'iProdId') {
            $yList = Ims::getProdByTypeId($prodType);
        } else {
            $yList = @self::$enum[$yType];
        }
        if (!$yList) {
            throw new \Exception('未定义y轴分类');
        }
        $sql = "select SUM(iInNum) as addNum,DATE_FORMAT(tCreateDate,'%Y-%m-%d') as tTime from XHIms_ProduceIn ";
        $conditionArr = [];
        $conditionArr[] = 'bDelete = 0 ';
        $prods = Ims::getProdByTypeId($prodType);
        if ($prod) {
            $conditionArr[] = " iProdId = {$prod} ";
        } elseif ($prodType) {
            $prodIds = array_keys($prods);
            if ($prodIds) {
                $conditionArr[] = " iProdId in (".implode(',', $prodIds).") ";
            }
        }
        if ($schoolId) {
            $conditionArr[] = "iSchoolId = '{$schoolId}' ";
        }
        $schoolIds = Customer::filterSchoolByArea([
            'province' => $province != '全部省' ? $province : '',
            'city' => $city != '全部市' ? $city : '',
            'county' => $county != '全部区' ? $county : '',
        ]);
        if ($schoolIds) {
            $conditionArr[] = " iSchoolId in (".implode(',', $schoolIds).") ";
        }
        if ($startTime) {
            $conditionArr[] = " DATE_FORMAT(tCreateDate,'%Y-%m-%d') >= '{$startTime}'  ";
        }
        if ($endTime) {
            $conditionArr[] = " DATE_FORMAT(tCreateDate,'%Y-%m-%d') <= '{$endTime}'  ";
        }

        $sql .= 'where ' . implode(' and ', $conditionArr);
        $source = [
            'series' => [],
        ];
        foreach ($yList as $index => $y) {
            $newSql = $sql;
            $newSql.= " and {$yType} =  {$index} ";
            $newSql .= " GROUP BY DATE_FORMAT(tCreateDate,'%Y-%m-%d') ";
            $data = $this->db->fetchAll($newSql);
            $source['series'][] = [
                'name' => $y,
                'source' => 'dataSource' . $index,
            ];
            $data = array_column($data, 'addNum', 'tTime');
            $source['dataSource' . $index] = $this->getXData($xList, $data, $statsType);
        }
        $source['xList'] = $statsType != 'day' ? array_keys($xList) : $xList;
        return $source;
    }

    /**
     * 获取出库数据
     *
     * @param $params
     *
     * @return array
     * @throws \Exception
     *
     * create by dale  20190115
     */
    public function getOutStats($params)
    {
        $startTime = Tool::getValue($params, 'startTime');
        $endTime   = Tool::getValue($params, 'endTime');
        $province  = Tool::getValue($params, 'province');
        $city      = Tool::getValue($params, 'city');
        $county    = Tool::getValue($params, 'county');
        $prodType  = Tool::getValue($params, 'prodType');
        $prod      = Tool::getValue($params, 'prod');
        $statsType = Tool::getValue($params, 'statsType');
        $yType     = Tool::getValue($params, 'yType');
        $schoolId  = Tool::getValue($params, 'schoolId');
        $xList = $this->getXList($statsType, $startTime, $endTime);
        if ($yType == 'iProdId') {
            $yList = Ims::getProdByTypeId($prodType);
        } else {
            $yList = @self::$enum[$yType];
        }
        if (!$yList) {
            throw new \Exception('未定义y轴分类');
        }
        $sql = "select SUM(iOutNum) as addNum,DATE_FORMAT(tCreateDate,'%Y-%m-%d') as tTime from XHIms_ProduceOut ";
        $conditionArr = [];
        $conditionArr[] = 'bDelete = 0 ';
        $prods = Ims::getProdByTypeId($prodType);
        if ($prod) {
            $conditionArr[] = " iProdId = {$prod} ";
        } elseif ($prodType) {
            $prodIds = array_keys($prods);
            if ($prodIds) {
                $conditionArr[] = " iProdId in (".implode(',', $prodIds).") ";
            }
        }
        if ($schoolId) {
            $conditionArr[] = "iSchoolId = '{$schoolId}' ";
        }
        $schoolIds = Customer::filterSchoolByArea([
            'province' => $province != '全部省' ? $province : '',
            'city' => $city != '全部市' ? $city : '',
            'county' => $county != '全部区' ? $county : '',
        ]);
        if ($schoolIds) {
            $conditionArr[] = " iSchoolId in (".implode(',', $schoolIds).") ";
        }
        if ($startTime) {
            $conditionArr[] = " DATE_FORMAT(tCreateDate,'%Y-%m-%d') >= '{$startTime}'  ";
        }
        if ($endTime) {
            $conditionArr[] = " DATE_FORMAT(tCreateDate,'%Y-%m-%d') <= '{$endTime}'  ";
        }

        $sql .= 'where ' . implode(' and ', $conditionArr);
        $source = [
            'series' => [],
        ];
        foreach ($yList as $index => $y) {
            $newSql = $sql;
            $newSql.= " and {$yType} =  {$index} ";
            $newSql .= " GROUP BY DATE_FORMAT(tCreateDate,'%Y-%m-%d') ";
            $data = $this->db->fetchAll($newSql);
            $source['series'][] = [
                'name' => $y,
                'source' => 'dataSource' . $index,
            ];
            $data = array_column($data, 'addNum', 'tTime');
            $source['dataSource' . $index] = $this->getXData($xList, $data, $statsType);
        }
        $source['xList'] = $statsType != 'day' ? array_keys($xList) : $xList;
        return $source;
    }




    /**
     * 处理x轴的数据
     *
     * @param $xList
     * @param $data
     * @param $statsType
     *
     * @return array
     * @throws \Exception
     *
     * create by dale  20190114
     */
    public function getXData($xList, $data, $statsType)
    {
        $customerArr = [];
        if ($statsType == 'day') {
            foreach ($xList as $index => $row) {
                $num = Tool::getValue($data, $row, 0);
                $customerArr[] = $num;
            }
            return $customerArr;
        } elseif (in_array($statsType, ['week', 'month', 'quarter', 'year'])) {
            foreach ($xList as $index => $row) {
                $dayArr   = explode('~', $row);
                $startDay = $dayArr[0];
                $endDay   = $dayArr[1];
                $total = 0;
                for ($i = strtotime($startDay); $i <= strtotime($endDay); $i += 24*3600) { //计算一周,一个月的数量
                    $num = Tool::getValue($data, date('Y-m-d', $i), 0);
                    $total += $num;
                }
                $customerArr[] = $total;
            }
            return $customerArr;
        } else {
            throw new \Exception('类型不匹配');
        }
    }



    /**
     * 获取x轴坐标 时间段
     *
     * @param $type
     * @param $startTime
     * @param $endTime
     * @return array   ['年'=>'开始时间~结束时间']
     * @throws \Exception
     *
     * create by dale 20180420
     */
    public function getXList($type, $startTime, $endTime)
    {
        try {
            $xList = [];
            if ($type == 'day') { //天
                for ($i = strtotime($startTime); $i <= strtotime($endTime); $i += 24 * 3600) {
                    $xList[] = date('Y-m-d', $i);
                }
                return $xList;
            }
            if ($type == 'week') { //周
                $startDate = strtotime($startTime);
                $endDate = strtotime($endTime);
                if (date("w", $startDate) != 1) {  //如果不为周一则取下周
                    $xList[date('Y', $startDate) . '年第' . intval(date('W', $startDate)) . '周'] = $startTime . '~' . date('Y-m-d', strtotime("this week sunday", $startDate));
                    $startDate = strtotime("next monday", $startDate);
                }
                $countWeek = ($endDate - $startDate) / (7 * 24 * 3600);
                for ($i = 0; $i < $countWeek; $i++) {
                    $startDay = date('Y-m-d', $startDate);
                    $endDay = date('Y-m-d', strtotime("+ 6 days", $startDate));
                    if ($endDay < $endTime) {
                        $xList[date('Y', $startDate) . '年第' . intval(date('W', $startDate)) . '周'] = $startDay . '~' . $endDay;
                        $startDate = strtotime("+ 1 day", strtotime($endDay));
                    } else {
                        $xList[date('Y', $startDate) . '年第' . intval(date('W', $startDate)) . '周'] = $startDay . '~' . $endTime;
                    }
                }
//                var_dump($xList);die;
                return $xList;
            }
            if ($type == 'month') { //月份
                $startDate = strtotime($startTime);
                $endDate = strtotime($endTime);
                $monthCount = (intval(date('Y', $endDate)) - intval(date('Y', $startDate))) * 12 + (intval(date('m', $endDate)) - intval(date('m', $startDate))) + 1;

                for ($i = 0; $i < $monthCount; $i++) {
                    $startDay = date('Y-m-d', $startDate);
                    if (date('m', $startDate) == 12) {
                        $endDay = date('Y-m-d', mktime(0, 0, 0, 12, 31, date('Y', $startDate)));
                    } else {
                        $endDay = date('Y-m-d', mktime(0, 0, 0, date('m', $startDate) + 1, 1, date('Y', $startDate)) - 24 * 3600);
                    }

                    if ($endDay < $endTime) {
                        $xList[date('Y', $startDate) . '年第' . intval(date('m', $startDate)) . '月'] = $startDay . '~' . $endDay;
                        $startDate = strtotime("+ 1 day", strtotime($endDay));
                    } else {
                        $xList[date('Y', $startDate) . '年第' . intval(date('m', $startDate)) . '月'] = $startDay . '~' . $endTime;
                    }
                }
                return $xList;
            }
            if ($type == 'quarter') { //季度
                $startDate = strtotime($startTime);
                $endDate = strtotime($endTime);
                $seasonCount = (intval(date('Y', $endDate)) - intval(date('Y', $startDate))) * 4 + (ceil((date('n', $endDate)) / 3) - ceil((date('n', $startDate)) / 3)) + 1;
                var_dump($seasonCount);
                for ($i = 0; $i < $seasonCount; $i++) {
                    $startDay = date('Y-m-d', $startDate);
                    $nowSeason = ceil((date('n', $startDate)) / 3);
                    $endDay = date('Y-m-d', mktime(0, 0, 0, $nowSeason * 3, date('t', mktime(0, 0, 0, $nowSeason * 3, 1, date("Y"))), date('Y', $startDate)));
                    if (strtotime($endDay) < strtotime($endTime)) {
                        $xList[date('Y', $startDate) . '年第' . $nowSeason . '季'] = $startDay . '~' . $endDay;
                        $startDate = strtotime("+ 1 day", strtotime($endDay));
                    } else {
                        $xList[date('Y', $startDate) . '年第' . $nowSeason . '季'] = $startDay . '~' . $endTime;
                    }
                }
                return $xList;
            }
            if ($type == 'year') {
                $startDate = strtotime($startTime);
                $endDate = strtotime($endTime);
                $yearCount = intval(date('Y', $endDate)) - intval(date('Y', $startDate)) + 1;
                for ($i = 0; $i < $yearCount; $i++) {
                    $startDay = date('Y-m-d', $startDate);
                    $endDay = date('Y-m-d', mktime(0, 0, 0, 12, 31, date('Y', $startDate)));
                    if (strtotime($endDay) < strtotime($endTime)) {
                        $xList[date('Y', $startDate) . '年'] = $startDay . '~' . $endDay;
                        $startDate = strtotime("+ 1 day", strtotime($endDay));
                    } else {
                        $xList[date('Y', $startDate) . '年'] = $startDay . '~' . $endTime;
                    }
                }
                return $xList;
            }
            throw new \Exception('类型不匹配');
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }


}